package com.grandbroad.eim.adapter;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easy.west.westclient.api.base.bean.VodBean;
import com.grandbroad.eim.R;
import com.grandbroad.eim.entry.LiveRoomBean;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class VideoDesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final int DESC_POSITION = 0;
    private final int VIDEO_LIST_POSITION = 1;

    private VodListAdpter listAdpter;
    private List<VodBean> vodBeanList = new ArrayList<>();
    private Object thisbean;
    private AppCompatActivity activity;
    private oncollectionClickListener oncollectionClickListener;
    private onFollowBtnClickListener followBtnClickListener;
    private onJuBaoBtnClickListener juBaoBtnClickListener;
    private onShareBtnClickListener shareBtnClickListener;
    private onlikeClickListener onlikeClickListener;
    private boolean follow = false;
    private boolean collect = false;

    public boolean isCollect() {
        return collect;
    }

    public void setCollect(boolean collect) {
        this.collect = collect;
        notifyDataSetChanged();
    }

    public boolean isFollow() {
        return follow;
    }

    public void setFollowBtnClickListener(onFollowBtnClickListener followBtnClickListener) {
        this.followBtnClickListener = followBtnClickListener;
    }

    public void setOncollectionClickListener(VideoDesAdapter.oncollectionClickListener oncollectionClickListener) {
        this.oncollectionClickListener = oncollectionClickListener;
    }

    public void setJuBaoBtnClickListener(onJuBaoBtnClickListener juBaoBtnClickListener) {
        this.juBaoBtnClickListener = juBaoBtnClickListener;
    }

    public void setShareBtnClickListener(onShareBtnClickListener shareBtnClickListener) {
        this.shareBtnClickListener = shareBtnClickListener;
    }

    public void setOnlikeClickListener(VideoDesAdapter.onlikeClickListener onlikeClickListener) {
        this.onlikeClickListener = onlikeClickListener;
    }


    public VideoDesAdapter(AppCompatActivity activity) {
        listAdpter = new VodListAdpter(activity);
    }

    public void setThisbean(Object thisbean) {
        this.thisbean = thisbean;
    }

    public void setVodBeanList(List<VodBean> vodBeanList) {
        this.vodBeanList = vodBeanList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = null;
        if (getItemViewType(i) == DESC_POSITION) {
            v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.describle_item, viewGroup, false);
            return new DescribleViewHolder(v);
        } else {
            v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_video_item, viewGroup, false);
            return new VideoListViewHoler(v);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (viewHolder instanceof DescribleViewHolder) {
            DescribleViewHolder describleViewHolder = (DescribleViewHolder) viewHolder;
            if (thisbean != null) {
                if (thisbean instanceof VodBean) {
                    VodBean vodBean = (VodBean) thisbean;

                    describleViewHolder.tvVideoName.setText(vodBean.getName());
                    describleViewHolder.tvVideoDesc.setText(vodBean.getDescrib());
                    describleViewHolder.tvVideoCount.setText("观看次数:" + vodBean.getCount());
                    x.image().bind(describleViewHolder.imgVideoAvatar, vodBean.getUser().getAvatar().toString());
                }
                if (thisbean instanceof LiveRoomBean) {
                    LiveRoomBean vodBean = (LiveRoomBean) thisbean;
                    describleViewHolder.tvVideoName.setText(vodBean.getUser().getName());
                    describleViewHolder.tvVideoDesc.setText(vodBean.getDescript());
                    //聊天室人数
                    x.image().bind(describleViewHolder.imgVideoAvatar, vodBean.getUser().getAvatar());
                }
                if (follow) {
                    describleViewHolder.tvFollowItem.setBackgroundResource(R.drawable.follew_bg);
                    describleViewHolder.tvFollowItem.setText("已关注");
                } else {
                    describleViewHolder.tvFollowItem.setBackgroundResource(R.drawable.not_follow_bg);
                    describleViewHolder.tvFollowItem.setText("关注");
                }
                if (collect) {
                    describleViewHolder.tvCollect.setText("已收藏");
                } else {
                    describleViewHolder.tvCollect.setText("收藏");
                }
            }

            //设置点击事件
            describleViewHolder.liCollection.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != oncollectionClickListener) {
                        oncollectionClickListener.click(i, thisbean);
                    }
                }
            });
            describleViewHolder.liLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != onlikeClickListener) {
                        onlikeClickListener.click(i, thisbean);
                    }
                }
            });
            describleViewHolder.liMute.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != juBaoBtnClickListener) {
                        juBaoBtnClickListener.click(i, thisbean);
                    }
                }
            });
            describleViewHolder.liShare.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != shareBtnClickListener) {
                        shareBtnClickListener.click(i, thisbean);
                    }
                }
            });
            describleViewHolder.tvFollowItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != followBtnClickListener) {
                        followBtnClickListener.click(i, thisbean);
                    }
                }
            });

        }
        if (viewHolder instanceof VideoListViewHoler) {
            VideoListViewHoler videoListViewHoler = (VideoListViewHoler) viewHolder;
            if (vodBeanList != null && vodBeanList.size() > 0) {
                videoListViewHoler.recyclerVideoList.setLayoutManager(new LinearLayoutManager(videoListViewHoler.itemView.getContext()));
                videoListViewHoler.recyclerVideoList.setAdapter(listAdpter);
                listAdpter.setVodList(vodBeanList);
                listAdpter.notifyDataSetChanged();
            }

        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return DESC_POSITION;
        } else {
            return VIDEO_LIST_POSITION;
        }

    }

    @Override
    public int getItemCount() {
        return 2;
    }

    public void setFollow(boolean follow) {
        this.follow = follow;
        notifyDataSetChanged();
    }

    /***
     * 描述
     */
    class DescribleViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_video_avatar)
        ImageView imgVideoAvatar;
        @BindView(R.id.tv_video_name)
        TextView tvVideoName;
        @BindView(R.id.tv_video_count)
        TextView tvVideoCount;
        @BindView(R.id.tv_follow_item)
        TextView tvFollowItem;
        @BindView(R.id.tv_mutu)
        TextView tvMutu;
        @BindView(R.id.li_mute)
        LinearLayout liMute;
        @BindView(R.id.img_collection)
        ImageView imgCollection;
        @BindView(R.id.tv_collect)
        TextView tvCollect;
        @BindView(R.id.li_collection)
        LinearLayout liCollection;
        @BindView(R.id.img_like)
        ImageView imgLike;
        @BindView(R.id.tv_like)
        TextView tvLike;
        @BindView(R.id.li_like)
        LinearLayout liLike;
        @BindView(R.id.tv_share)
        TextView tvShare;
        @BindView(R.id.li_share)
        LinearLayout liShare;
        @BindView(R.id.tv_video_desc)
        TextView tvVideoDesc;
        public DescribleViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class VideoListViewHoler extends RecyclerView.ViewHolder {
        @BindView(R.id.recycler_video_list)
        RecyclerView recyclerVideoList;

        public VideoListViewHoler(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 关注按钮点击事件
     */
    public interface onFollowBtnClickListener {
        void click(int pos, Object o);
    }

    /**
     * 举报点击事件
     */
    public interface onJuBaoBtnClickListener {
        void click(int pos, Object o);
    }

    /**
     * 点赞点击事件
     */
    public interface onlikeClickListener {
        void click(int pos, Object o);
    }

    /**
     * 收藏点击事件
     */
    public interface oncollectionClickListener {
        void click(int pos, Object o);
    }

    /**
     * 分享点击事件
     */
    public interface onShareBtnClickListener {
        void click(int pos, Object o);
    }


}
